<?php
/**
 * 文件说明： 课时安排后台管理模块
 * ============================================================================
 * 版权所有 (C) 2010-2011 微凡工作室 | 微凡网络，并保留所有权利。
 * 网站地址: http://www.admin80.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 微凡工作室 | 微凡网络 $
 * $Encoding: UTF-8 $
 * $Id: admin_courses.php  2011-3-27 下午08:53:16Z 微凡工作室 | 微凡网络 $
*/

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form', '', 0);

class admin_courses extends admin {

	private $db; 
	public $userid;
	public $username;
	public $admin_email;
	public $site_id;
	private $member_db;
	private $admin_db;
	private $courses;
	private $classes;
	public function __construct() {
		parent::__construct();
		$this->username = param::get_cookie('admin_username');
		$this->userid = param::get_cookie('userid');
		$this->admin_email = param::get_cookie('admin_email');
		$this->site_id = $this->get_siteid();
		$this->db = pc_base::load_model('courses_model');
	}
	
	public function init() {
		//获取课程列表
		$this->courses = $this->get_courses_array(false, true);
		if (empty($this->courses))
		{
			showmessage(L('courses_null'), 'index.php?m=courses&c=admin_courses&a=courseadd&menuid='.$_GET['menuid']);
		}		
		//获取班级列表
		$this->classes = $this->get_classes_array(false, true);
		if (empty($this->classes))
		{
			showmessage(L('classes_null'), 'index.php?m=courses&c=admin_courses&a=classadd&menuid='.$_GET['menuid']);
		}
		//课时列表
		$sql = '';		
		$sql = '`siteid`=\''.$this->site_id.'\'';		
		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`ctime` DESC, `ctid` DESC', $page);
		if (!empty($data))
		{
			foreach ($data as $key=>$val)
			{
				$val['csid'] = $this->courses[$val['csid']];
				$val['cid'] = $this->classes[$val['cid']];
				$val['unedit'] = 1;//$val['ctime']<SYS_TIME ? 0 : 1;
				$val['ctime'] = $this->transfer_timestamp($val['ctime']);
				$val['ctused'] = $val['ctused'] ? L('used') : L('unused');
				$val['student_join_nums'] = $this->get_course_time_count($val['ctid'], 'student');
				$val['teacher_join_nums'] = $this->get_course_time_count($val['ctid'], 'teacher');
				$data[$key] = $val;
			}
		}		

		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=courses&c=admin_courses&a=add&menuid='.$_GET['menuid'].'\', title:\''.L('coursestime_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('coursestime_add'));
		include $this->admin_tpl('coursestime_list');
	}
	
	/**
	 * 添加课程
	 */
	public function courseadd()
	{
		if(isset($_POST['dosubmit'])) {
			//数据提交
			$_POST['courses'] = $this->check_courses($_POST['courses']);
			if($this->db->insert($_POST['courses'])) showmessage(L('courses_add_succ'), HTTP_REFERER);
		}
		else 
		{
			pc_base::load_app_func('global', 'admin');
			$show_validator = $show_scroll = 1;
			pc_base::load_sys_class('form', '', 0);
			$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=courses&c=admin_courses&a=add&menuid='.$_GET['menuid'].'\', title:\''.L('coursestime_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('coursestime_add'));
			include $this->admin_tpl('courses_add');
		}
	}
	
	/**
	 * 编辑课程名称
	 */
	public function courseedit(){
		$_GET['csid'] = intval($_GET['csid']);
		if(!$_GET['csid']) showmessage(L('illegal_operation'));
		if(isset($_POST['dosubmit'])) {
			$_POST['courses'] = $this->check_courses($_POST['courses'], 'edit');
			if($this->db->update($_POST['courses'], array('csid' => $_GET['csid']))) showmessage(L('courses_edit_succ'), HTTP_REFERER, '', 'edit');
		} else {
			$where = array('csid' => $_GET['csid']);
			$this->db->set_model(2);
			$pc_info = $this->db->get_one($where);
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('courses_edit');
		}
	}
	
	/**
	 * 课程列表
	 */
	public function courselist() {
		$sql = '';		
		$sql = '`siteid`=\''.$this->site_id.'\'';		
		$page = max(intval($_GET['page']), 1);
		$this->db->set_model(2);
		$data = $this->db->listinfo($sql, '`csid` DESC', $page);
		if (!empty($data))
		{
			foreach ($data as $key=>$val)
			{
				$val['addtime'] = $this->transfer_timestamp($val['addtime']);
				$val['csused'] = $val['csused'] ? L('used') : L('unused');
				$val['student_join_nums'] = $this->get_course_time_count($val['csid'], 'student', 'course');
				$val['teacher_join_nums'] = $this->get_course_time_count($val['csid'], 'teacher', 'course');
				$data[$key] = $val;
			}
		}		

		$big_menu = array('javascript:window.top.art.dialog({idctused:\'add\',iframe:\'?m=courses&c=admin_courses&a=add&menuid='.$_GET['menuid'].'\', title:\''.L('coursestime_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('coursestime_add'));
		$this->db->set_model();
		include $this->admin_tpl('courses_list');
	}
	
	/**
	 * 删除课程
	 */
	public function coursedel()
	{//echo $this->db->table_name;die;
		$_GET['csid'] = intval($_GET['csid']);
		if(!$_GET['csid']) showmessage(L('illegal_operation'));
		//删除课程表
		$this->db->set_model(2);
		$this->db->delete(array('csid'=>$_GET['csid']));
		//删除课时表
		$this->db->set_model(0);
		$this->db->delete(array('csid'=>$_GET['csid']));
		//删除学生绑定表
		$this->db->set_model(4);
		$this->db->delete(array('course_id'=>$_GET['csid']));
		//删除教师绑定表
		$this->db->set_model(3);
		$this->db->delete(array('course_id'=>$_GET['csid']));
		
		showmessage(L('operation_success'),'?m=courses&c=admin_courses');
	}
	
	/**
	 * 添加班级
	 */
	public function classadd()
	{
		if(isset($_POST['dosubmit'])) {
			//数据提交
			$_POST['classes'] = $this->check_classes($_POST['classes']);
			if($this->db->insert($_POST['classes'])) showmessage(L('classes_add_succ'), HTTP_REFERER);
		}
		else 
		{
			pc_base::load_app_func('global', 'admin');
			$show_validator = $show_scroll = 1;
			pc_base::load_sys_class('form', '', 0);
			$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=courses&c=admin_courses&a=add&menuid='.$_GET['menuid'].'\', title:\''.L('coursestime_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('coursestime_add'));
			include $this->admin_tpl('classes_add');
		}
	}
	
	/**
	 * 编辑班级名称
	 */
	public function classedit(){
		$_GET['cid'] = intval($_GET['cid']);
		if(!$_GET['cid']) showmessage(L('illegal_operation'));
		if(isset($_POST['dosubmit'])) {
			$_POST['classes'] = $this->check_classes($_POST['classes'], 'edit');
			if($this->db->update($_POST['classes'], array('cid' => $_GET['cid']))) showmessage(L('classes_edit_succ'), HTTP_REFERER, '', 'edit');
		} else {
			$where = array('cid' => $_GET['cid']);
			$this->db->set_model(1);
			$pc_info = $this->db->get_one($where);
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('classes_edit');
		}
	}
	
	/**
	 * 班级列表
	 */
	public function classlist() {
		$sql = '';		
		$sql = '`siteid`=\''.$this->site_id.'\'';		
		$page = max(intval($_GET['page']), 1);
		$this->db->set_model(1);
		$data = $this->db->listinfo($sql, '`cid` DESC', $page);
		if (!empty($data))
		{
			foreach ($data as $key=>$val)
			{
				$val['addtime'] = $this->transfer_timestamp($val['addtime']);
				$val['cused'] = $val['cused'] ? L('used') : L('unused');
				$val['student_join_nums'] = $this->get_course_time_count($val['cid'], 'student', 'class');
				$val['teacher_join_nums'] = $this->get_course_time_count($val['cid'], 'teacher', 'class');
				$data[$key] = $val;
			}
		}		

		$big_menu = array('javascript:window.top.art.dialog({idctused:\'add\',iframe:\'?m=courses&c=admin_courses&a=add&menuid='.$_GET['menuid'].'\', title:\''.L('coursestime_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('coursestime_add'));
		$this->db->set_model();
		include $this->admin_tpl('classes_list');
	}
	
	/**
	 * 删除班级
	 */
	public function classdel()
	{//echo $this->db->table_name;die;
		$_GET['cid'] = intval($_GET['cid']);
		if(!$_GET['cid']) showmessage(L('illegal_operation'));
		//删除班级表
		$this->db->set_model(1);
		$this->db->delete(array('cid'=>$_GET['cid']));
		//删除课时表
		$this->db->set_model(0);
		$this->db->delete(array('cid'=>$_GET['cid']));
		//删除学生绑定表
		$this->db->set_model(4);
		$this->db->delete(array('class_id'=>$_GET['cid']));
		//删除教师绑定表
		$this->db->set_model(3);
		$this->db->delete(array('class_id'=>$_GET['cid']));
		
		showmessage(L('operation_success'),'?m=courses&c=admin_courses');
	}
	
	/**
	 * 添加课时安排信息
	 */
	public function add() {
		//获取可用的课程列表
		$this->courses = $this->get_courses_array(true, true);
		if (empty($this->courses))
		{
			showmessage(L('courses_name_used'));
			//showmessage(L('courses_name_used'), 'index.php?m=courses&c=admin_courses&a=courseadd&menuid='.$_GET['menuid']);
		}		
		//获取可用的班级列表
		$this->classes = $this->get_classes_array(true, true);
		if (empty($this->classes))
		{
			showmessage(L('classes_name_used'));
			//showmessage(L('classes_name_used'), 'index.php?m=courses&c=admin_courses&a=classadd&menuid='.$_GET['menuid']);
		}
		if(isset($_POST['dosubmit'])) {
			$_POST['coursestime'] = $this->check_coursestime($_POST['coursestime']);
			if($this->db->insert($_POST['coursestime'])) showmessage(L('coursestime_add_succ'), HTTP_REFERER, '', 'add');
		} else {
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$show_header = $show_validator = $show_scroll = 1;
			$nowtime_str = date("Y-m-d H:i", SYS_TIME).':00';
			include $this->admin_tpl('coursestime_add');
		}
	}
	
	/**
	 * 修改课时安排信息
	 */
	public function edit() {
		$_GET['ctid'] = intval($_GET['ctid']);
		if(!$_GET['ctid']) showmessage(L('illegal_operation'));	
		//获取可用的课程列表
		$this->courses = $this->get_courses_array(true, true);
		if (empty($this->courses))
		{
			showmessage(L('courses_name_used'));
			//showmessage(L('courses_name_used'), 'index.php?m=courses&c=admin_courses&a=courseadd&menuid='.$_GET['menuid']);
		}		
		//获取可用的班级列表
		$this->classes = $this->get_classes_array(true, true);
		if (empty($this->classes))
		{
			showmessage(L('classes_name_used'));
			//showmessage(L('classes_name_used'), 'index.php?m=courses&c=admin_courses&a=classadd&menuid='.$_GET['menuid']);
		}
		if(isset($_POST['dosubmit'])) {
			$_POST['coursestime'] = $this->check_coursestime($_POST['coursestime'], 'edit');
			if($this->db->update($_POST['coursestime'], array('ctid' => $_GET['ctid']))) showmessage(L('coursestime_edit_succ'), HTTP_REFERER, '', 'edit');
		} else {
			$where = array('ctid' => $_GET['ctid']);
			$pc_info = $this->db->get_one($where);
			$pc_info['format_endtime'] = $this->transfer_timestamp($pc_info['ctime']);
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('coursestime_edit');
		}
	}
	
	/**
	 * 删除课时安排
	 */
	public function delete()
	{
		$_GET['ctid'] = intval($_GET['ctid']);
		if(!$_GET['ctid']) showmessage(L('illegal_operation'));
		$this->db->delete(array('ctid'=>$_GET['ctid']));
		//删除学生绑定表
		$this->db->set_model(4);
		$this->db->delete(array('course_time_id'=>$_GET['ctid']));
		//删除教师绑定表
		$this->db->set_model(3);
		$this->db->delete(array('course_time_id'=>$_GET['ctid']));		
		showmessage(L('operation_success'),'?m=courses&c=admin_courses');
	}
	
	/**
	 * 绑定列表(学生和教师二合一)
	 */
	public function bind_list()
	{
		//会员模型
		$member_models = array('10' => '学生列表', '11' => '教师列表');
		//课时列表
		$ct_list = $this->get_course_time_array();
		//审核状态
		$check_list = array('1'=>'通过', '2'=>'待审核');
		//设置默认的搜索模型
		$search_modelid = intval($_REQUEST['search_modelid']);
		$search_modelid = @array_key_exists($search_modelid, $member_models) ? $search_modelid : 10;
		$search_ctid = intval($_REQUEST['search_ctid']);
		$search_check = intval($_REQUEST['search_check']);
		$sql_check = $search_check == 1 ? '1' : ($search_check == 2 ? '0' : '-1');
		
		$field = '';
		$field_id = '';
		if ($search_modelid == 10)
		{
			$this->db->set_model(4);
			$field = 'student_id';
			$field_id = 'sid';
		}
		else 
		{
			$this->db->set_model(3);
			$field = 'teacher_id';
			$field_id = 'ctid';
		}
		
		//单个处理
		if (isset($_GET['dt']))
		{
			$dt = strtolower(trim(strip_tags($_GET['dt'])));
			$bid = intval($_GET['bid']);
			if ($bid<1 || @!in_array($dt, array('agree', 'refuse', 'delete'))) showmessage("参数无效，请返回");
			//获得记录信息
			$record_info = $this->db->get_one(array($field_id=>$bid));
			if (empty($record_info)) showmessage("抱歉，记录不存在");
			//如果已经同意，就不能执行拒绝操作
			if ($record_info['is_allow'] && $dt == 'refuse') showmessage("该记录已经审核通过，不能拒绝");
			//消息表
			$message_db = pc_base::load_model('message_model');
			$message_to_username = get_memberinfo($record_info[$field], 'username');
			$course_time_names = $this->get_special_course_time($record_info['course_time_id']);
			$course_class_name = $course_time_names['course_class_name'];
			
			if ($search_modelid == 10)
			{
				$this->db->set_model(4);
			}
			else 
			{
				$this->db->set_model(3);
			}
			//开始执行最终操作
			if ($dt == 'agree')
			{
				$this->db->update(array('is_allow' => 1), array($field_id => $bid));
				$subject = "[{$course_class_name}] 绑定通知：已同意";
				$content = "亲爱的{$message_to_username}：很荣幸的通知您，我们已经通过了绑定[{$course_class_name}]的申请";
				$message_db->add_message($message_to_username,'SYSTEM',$subject,$content,true);
				showmessage(L('operation_success'),HTTP_REFERER);
			}
			elseif ($dt == 'refuse')
			{
				$this->db->delete(array($field_id => $bid));
				$subject = "[{$course_class_name}] 绑定通知：已拒绝";
				$content = "亲爱的{$message_to_username}：很抱歉的通知您，我们拒绝了您绑定[{$course_class_name}]的申请，相关申请记录已删除";
				$message_db->add_message($message_to_username,'SYSTEM',$subject,$content,true);
				showmessage(L('operation_success'),HTTP_REFERER);
			}
			elseif ($dt == 'delete') 
			{
				$this->db->delete(array($field_id => $bid));
				$subject = "[{$course_class_name}] 绑定通知：已删除";
				$content = "亲爱的{$message_to_username}：很抱歉的通知您，我们删除了您绑定[{$course_class_name}]的记录";
				$message_db->add_message($message_to_username,'SYSTEM',$subject,$content,true);
				showmessage(L('operation_success'),HTTP_REFERER);
			}			
		}
		
		//批量处理
		if (isset($_POST['submit']))
		{
			$bind_ids = $_POST['bind_ids'];
			$op_value = intval($_POST['op_value']);
			$op_value = $op_value>0 ? 1 : 0;
			if (!$bind_ids) showmessage("没有要处理的数据");
			$message_db = pc_base::load_model('message_model');
			foreach ($bind_ids as $bid)
			{
				$record_info = $this->db->get_one(array($field_id=>$bid));
				if (empty($record_info)) continue;
				if ($record_info['is_allow']) continue;
				$message_to_username = get_memberinfo($record_info[$field], 'username');
				$course_time_names = $this->get_special_course_time($record_info['course_time_id']);
				$course_class_name = $course_time_names['course_class_name'];
				
				if ($search_modelid == 10)
				{
					$this->db->set_model(4);
				}
				else 
				{
					$this->db->set_model(3);
				}
				if ($op_value)
				{
					$this->db->update(array('is_allow' => 1), array($field_id => $bid));					
					$subject = "[{$course_class_name}] 绑定通知：已同意";
					$content = "亲爱的{$message_to_username}：很荣幸的通知您，我们已经通过了绑定[{$course_class_name}]的申请";
					$message_db->add_message($message_to_username,'SYSTEM',$subject,$content,true);
				}
				else 
				{
					$this->db->delete(array($field_id => $bid));
					$subject = "[{$course_class_name}] 绑定通知：已拒绝";
					$content = "亲爱的{$message_to_username}：很抱歉的通知您，我们拒绝了您绑定[{$course_class_name}]的申请，相关申请记录已删除";
					$message_db->add_message($message_to_username,'SYSTEM',$subject,$content,true);
				}
			}
			showmessage(L('operation_success'),HTTP_REFERER);
		}
		
		//绑定列表获取
		$sql = '';		
		$sql .= $search_ctid>0 ? ' `course_time_id`=\''.$search_ctid.'\' ' : '';
		$sql .= $sql_check > -1 ? ((empty($sql) ? '' : ' AND ').' `is_allow`=\''.$sql_check.'\' ') : '';		
		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`add_time` DESC', $page);
		if ($data)
		{
			foreach ($data as $dkey=>$dval)
			{
				$data[$dkey]['bind_id'] = $dval[$field_id];
				$data[$dkey]['ct_name'] = $ct_list[$dval['course_time_id']];
				$data[$dkey]['user_name'] = get_memberinfo($dval[$field], 'username');
				//$data[$dkey]['user_id'] = get_memberinfo($dval[$field], 'userid');
				$data[$dkey]['apply_time'] = date('Y-m-d H:i:s', $dval['add_time']);
			}
		}
		
		//还原模型
		$this->db->set_model();
		
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=courses&c=admin_courses&a=add&menuid='.$_GET['menuid'].'\', title:\''.L('coursestime_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('coursestime_add'));
		include $this->admin_tpl('bind_list');
	}
	
	/**
	 * 数据统计(学生和教师二合一)
	 */
	public function bind_statistic()
	{
		//会员模型
		$member_models = array('10' => '学生列表', '11' => '教师列表');
		//班级列表
		$class_list = $this->get_classes_array(false, true);
		//课程列表
		$course_list = $this->get_courses_array(false, true);
		
		//设置默认的搜索模型
		$search_modelid = intval($_REQUEST['search_modelid']);
		$search_modelid = @array_key_exists($search_modelid, $member_models) ? $search_modelid : 10;
		$search_class = intval($_POST['search_class']);
		$search_course = intval($_POST['search_course']);
		
		$field = '';
		$field_id = '';
		if ($search_modelid == 10)
		{
			$this->db->set_model(4);
			$field = 'student_id';
			$field_id = 'sid';
		}
		else 
		{
			$this->db->set_model(3);
			$field = 'teacher_id';
			$field_id = 'ctid';
		}
		
		//获取列表
		$sql = '';
		$sql .= $search_class>0 ? " `class_id`='{$search_class}' " : "";
		$sql .= $search_course>0 ? (empty($sql) ? '' : ' AND ')." `course_id`='{$search_course}' " : "";
		
		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo_group($sql, " `{$field}` ASC ", $page);
		if ($data)
		{
			foreach ($data as $dkey => $dval)
			{
				$data[$dkey]['user_id'] = $dval[$field];
				$data[$dkey]['user_name'] = get_memberinfo($dval[$field], 'username');
			}
		}
		
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=courses&c=admin_courses&a=add&menuid='.$_GET['menuid'].'\', title:\''.L('coursestime_add').'\', width:\'700\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('coursestime_add'));
		include $this->admin_tpl('bind_statistic');
	}
	
	/**
	 * ajax检测课程名称是否重复
	 */
	public function check_course_name() {
		if (!$_GET['csname']) exit(0);
		if (CHARSET=='gbk') {
			$_GET['csname'] = iconv('UTF-8', 'GBK', $_GET['csname']);
		}
		$csname = $_GET['csname'];
		$this->db->set_model(2);
		if ($_GET['csid']) {
			$r = $this->db->get_one(array('csid' => $_GET['csid']));
			if ($r['csname'] == $csname) {
				$this->db->set_model();
				exit('1');
			}
		} 
		$r = $this->db->get_one(array('siteid' => $this->site_id, 'csname' => $csname), 'csid');
		$this->db->set_model();
		if($r['csid']) {
			exit('0');
		} else {
			exit('1');
		}
	}
	
	/**
	 * ajax检测班级名称是否重复
	 */
	public function check_classes_name() {
		if (!$_GET['cname']) exit(0);
		if (CHARSET=='gbk') {
			$_GET['cname'] = iconv('UTF-8', 'GBK', $_GET['cname']);
		}
		$cname = $_GET['cname'];
		$this->db->set_model(1);
		if ($_GET['cid']) {
			$r = $this->db->get_one(array('cid' => $_GET['cid']));
			if ($r['cname'] == $cname) {
				$this->db->set_model();
				exit('1');
			}
		} 
		$r = $this->db->get_one(array('siteid' => $this->site_id, 'cname' => $cname), 'cid');
		$this->db->set_model();
		if($r['cid']) {
			exit('0');
		} else {
			exit('1');
		}
	}
	
	/**
	 * 验证课程表单数据
	 * @param  		array 		$data 表单数组数据
	 * @param  		string 		$a 当表单为添加数据时，自动补上缺失的数据。
	 * @return 		array 		验证后的数据
	 */
	private function check_courses($data = array(), $a = 'add') {
		if($data['csname']=='') showmessage(L('courses_name_empty'));
		$data['csused'] = intval($data['csused']);
		$data['csused'] = in_array($data['csused'], array(0,1)) ? $data['csused'] : 0;
		$this->db->set_model(2);
		$r = $this->db->get_one(array('csname' => $data['csname']));
		if ($a=='add') {
			if (is_array($r) && !empty($r)) {
				showmessage(L('courses_name_exist'), HTTP_REFERER);
			}
			$data['siteid'] = $this->site_id;
			$data['addtime'] = SYS_TIME;
			$data['updatetime'] = 0;
			$data['admin_id'] = $this->userid;
		} else {
			if ($r['csid'] && ($r['csid']!=$_GET['csid'])) {
				showmessage(L('courses_name_exist'), HTTP_REFERER);
			}
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}
	
	/**
	 * 验证班级表单数据
	 * @param  		array 		$data 表单数组数据
	 * @param  		string 		$a 当表单为添加数据时，自动补上缺失的数据。
	 * @return 		array 		验证后的数据
	 */
	private function check_classes($data = array(), $a = 'add') {
		if($data['cname']=='') showmessage(L('classes_name_empty'));
		$data['cused'] = intval($data['cused']);
		$data['cused'] = in_array($data['cused'], array(0,1)) ? $data['cused'] : 0;
		$this->db->set_model(1);
		$r = $this->db->get_one(array('cname' => $data['cname']));
		if ($a=='add') {
			if (is_array($r) && !empty($r)) {
				showmessage(L('classes_name_exist'), HTTP_REFERER);
			}
			$data['siteid'] = $this->site_id;
			$data['addtime'] = SYS_TIME;
			$data['updatetime'] = 0;
			$data['admin_id'] = $this->userid;
		} else {
			if ($r['cid'] && ($r['cid']!=$_GET['cid'])) {
				showmessage(L('classes_name_exist'), HTTP_REFERER);
			}
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}
	
	/**
	 * 验证班级表单数据
	 * @param  		array 		$data 表单数组数据
	 * @param  		string 		$a 当表单为添加数据时，自动补上缺失的数据。
	 * @return 		array 		验证后的数据
	 */
	private function check_coursestime($data = array(), $a = 'add')
	{
		$data['csid'] = intval($data['csid']);
		if ($data['csid']<1) showmessage(L('courses_select'));
		$data['cid'] = intval($data['cid']);
		if ($data['cid']<1) showmessage(L('classes_select'));
		$data['ctime'] = trim(strip_tags($data['ctime']));
		if (empty($data['ctime'])) showmessage(L('coursestime_select'));
		
		if (!array_key_exists($data['csid'], $this->courses)) showmessage(L('courses_unvalid'));
		if (!array($data['cid'], $this->classes)) showmessage(L('classes_unvalid'));
		$data['ctime'] = strtotime($data['ctime']);
		if ($data['ctime']<SYS_TIME) showmessage(L('time_lower'));
		$data['cnote'] = trim(strip_tags($data['cnote']));
		
		$data['ctused'] = intval($data['ctused']);
		$data['ctused'] = in_array($data['ctused'], array(0,1)) ? $data['ctused'] : 0;
		
		$r = $this->db->get_one(array('csid' => $data['csid'], 'cid' => $data['cid'], 'ctime' => $data['ctime']));
		if ($a=='add') {
			if (is_array($r) && !empty($r)) {
				showmessage(L('coursestime_exist'), HTTP_REFERER);
			}
			$data['siteid'] = $this->site_id;
			$data['addtime'] = SYS_TIME;
			$data['updatetime'] = 0;
			$data['admin_id'] = $this->userid;
		} else {
			if ($r['ctid'] && ($r['ctid']!=$_GET['ctid'])) {
				showmessage(L('coursestime_exist'), HTTP_REFERER);
			}
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}
	
	/**
	 * 获得所有者的用户名
	 * 
	 * @param int $uid
	 */
	private function get_owner_username($uid = 0)
	{
		$member_username = '';
		$members = array();
		$this->member_db = pc_base::load_model('member_model');
		$members = $this->member_db->get_one(array('userid' => $uid), 'username');
		if (!empty($members))
		{
			$member_username = $members['username'];
		}		
		return $member_username;
	}
	
	/**
	 * 获得管理员的用户名
	 * 
	 * @param int $uid
	 */
	private function get_admin_username($admin_id = 0)
	{
		$admin_username = '';
		$admins = array();
		$this->admin_db = pc_base::load_model('admin_model');
		$admins = $this->admin_db->get_one(array('userid' => $admin_id), 'username');
		if (!empty($admins))
		{
			$admin_username = $admins['username'];
		}		
		return $admin_username;
	}
	
	/**
	 * 转换数字时间串到可识别的日期串
	 * 
	 * @param int $timestamp
	 */
	private function transfer_timestamp ($timestamp = 0, $format = "Y-m-d H:i:s")
	{
		$time_date = '';
		if ($timestamp>0)
		{
			$time_date = date($format, $timestamp);
		}	
		return $time_date;
	}
	
	/**
	 * 获得指定课时的相关信息
	 */
	private function get_special_course_time($ctid)
	{
		//获得可用的班级列表
		$classes = $this->get_classes_array(false, true);
		//获得可用的课程列表
		$courses = $this->get_courses_array(false, true);
		$this->db->set_model();
		$return = $this->db->get_one(array('ctid' => $ctid));
		if ($return)
		{
			$return['class_name'] = $classes[$return['cid']];
			$return['course_name'] = $courses[$return['csid']];
			$return['course_class_name'] = $return['class_name'].'  '.$return['course_name'];
		}
		$this->db->set_model();
		return $return;
	}
	
	/**
	 * 获得课时列表数组
	 */
	private function get_course_time_array($is_used = false, $is_enable = false)
	{
		$return = array();
		$this->db->set_model(0);
		$nowTime = SYS_TIME;
		$where = " siteid='{$this->site_id}' ".($is_used ? " AND ctused='1' " : "").($is_enable ? " AND ctime>'$nowTime' " : "");
		$query = $this->db->select($where);
		if ($query)
		{
			//获得可用的班级列表
			$classes = $this->get_classes_array(false, true);
			//获得可用的课程列表
			$courses = $this->get_courses_array(false, true);
			
			foreach ($query as $key=>$val)
			{
				//如果课程或者班级不在可用列表里面，不显示
				if (!@array_key_exists($val['csid'], $courses) || !@array_key_exists($val['cid'], $classes))
				{
					continue;
				}
				$return[$val['ctid']] = $classes[$val['cid']].'  '.$courses[$val['csid']];
			}
		}
		$this->db->set_model();
		return $return;
	}
	
	/**
	 * 获得课程列表数组
	 */
	private function get_courses_array($only_used = false, $single_item = false)
	{
		$return = array();
		//设置表明为课程表
		$this->db->set_model(2);
		$result = $only_used ? $this->db->select(array('siteid'=>$this->site_id, 'csused'=>'1')) : $this->db->select(array('siteid'=>$this->site_id));
		if ($result)
		{
			foreach ($result as $csval)
			{
				$return[$csval['csid']] = $single_item ? $csval['csname'] : $csval;
			}
			unset($result);
		}
		$this->db->set_model(0);
		return $return;
	}
	
	/**
	 * 获得班级列表数组
	 */
	private function get_classes_array($only_used = false, $single_item = false)
	{
		$return = array();
		//设置表明为课程表
		$this->db->set_model(1);
		$result = $only_used ? $this->db->select(array('siteid'=>$this->site_id, 'cused'=>'1')) : $this->db->select(array('siteid'=>$this->site_id));
		if ($result)
		{
			foreach ($result as $cval)
			{
				$return[$cval['cid']] = $single_item ? $cval['cname'] : $cval;
			}
			unset($result);
		}
		$this->db->set_model(0);
		return $return;
	}
	
	/**
	 * 课程数据统计
	 */
	private function get_course_time_count($ctid, $type = 'student', $field = '', $is_bind = -1)//学生和教师采用一个函数即可
	{
		$count = 0;
		$sql_model_id = $type == 'student' ? 4 : 3;
		$sql_field = $field == 'class' ? "class_id" : ($field == 'course' ? "course_id" : "course_time_id");
		$this->db->set_model($sql_model_id);
		$is_bind = intval($is_bind);
		$is_bind = $is_bind<0 ? -1 : ($is_bind>0 ? 1 : 0);
		$where = $is_bind<0 ? array($sql_field => $ctid) : array($sql_field => $ctid, 'is_allow' => $is_bind);
		$count = $this->db->count($where);
		$this->db->set_model();
		return $count;
	}
}
?>